package rm.mp3player;

import java.io.StringReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;

import rm.download.HttpDownloader;
import rm.model.Mp3Info;
import rm.xml.Mp3ListContentHandler;
import android.app.Activity;
import android.app.ListActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.SimpleAdapter;

public class Mp3ListActivity extends ListActivity {
    /** Called when the activity is first created. */
	private static final int UPDATE = 1;
	private static final int ABOUT = 2;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        updateListView();
    }
    
    /**
     * 菜单
     */

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// TODO Auto-generated method stub
		menu.add(0,UPDATE,1,R.string.mp3list_update);
		menu.add(0,ABOUT,2,R.string.mp3list_about);
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		System.out.println("item ID==>"+item);
		if(item.getItemId() == UPDATE){
			updateListView();
		}
		else if (item.getItemId() == ABOUT){
			//用户点击了关于按钮
		}
		return super.onOptionsItemSelected(item);
	}
	
	private void updateListView(){
		//用户点击了更新列表按钮
		String xml = downloadXML("http://192.168.2.89:8080/mp3/resources.xml");
		List<Mp3Info> mp3Infos = parse(xml);
		System.out.println("xml---->"+xml);	
	    setListAdapter(buildSimpleAdapter(mp3Infos));
	}

    private String downloadXML(String urlStr){
    	//urlStr = "http://192.168.2.89:8080/mp3/resources.xml"
    	HttpDownloader httpDownloader = new HttpDownloader();
    	String result = httpDownloader.download(urlStr);
		return result;
    }
    
	private List<Mp3Info> parse (String xmlStr){
		SAXParserFactory saxParseFactory = SAXParserFactory.newInstance();
		try{
			XMLReader xmlReader = saxParseFactory.newSAXParser().getXMLReader();
			List<Mp3Info> infos = new ArrayList<Mp3Info>();
			Mp3ListContentHandler mp3ListContentHandler = new Mp3ListContentHandler(infos);
			xmlReader.setContentHandler(mp3ListContentHandler);
			xmlReader.parse(new InputSource(new StringReader(xmlStr)));
			for(Iterator iterator = infos.iterator();iterator.hasNext();){
				Mp3Info mp3Info = (Mp3Info) iterator.next();
				System.out.println(mp3Info);
			}
		}
		catch(Exception e){
			e.printStackTrace();
		}
		return null;
	}
	private SimpleAdapter buildSimpleAdapter(List<Mp3Info> mp3Infos){
		// 生成一个List对象，并按照SimpleAdapter的标准，将mp3Infos当中的数据添加到List当中去
		List<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();
		for (Iterator iterator = mp3Infos.iterator(); iterator.hasNext();) {
			Mp3Info mp3Info = (Mp3Info) iterator.next();
			HashMap<String, String> map = new HashMap<String, String>();
			map.put("mp3_name", mp3Info.getMp3Name());
			map.put("mp3_size", mp3Info.getMp3Size());
			list.add(map);
		}
		// 创建一个SimpleAdapter对象
		SimpleAdapter simpleAdapter = new SimpleAdapter(this, list,
				R.layout.mp3info_item, new String[] { "mp3_name", "mp3_size" },
				new int[] { R.id.mp3_name, R.id.mp3_size });
		// 将这个SimpleAdapter对象设置到ListActivity当中
		return simpleAdapter;
	}
}